package at.moop.eneo;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.Toast;
import at.moop.eneo.service.SubmitterService;
import at.moop.eneo.util.Properties;

/**
 * This is the app's main activity which displays two buttons.
 */
public class EneoActivity extends Activity implements OnClickListener, OnSharedPreferenceChangeListener {
    SharedPreferences prefs;
    
    ImageButton serviceButton, settingsButton;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
                
        setContentView(R.layout.main);
        
        serviceButton = (ImageButton) findViewById(R.id.serviceButton);
        settingsButton = (ImageButton) findViewById(R.id.settingsButton);
        
        serviceButton.setOnClickListener(this);
        settingsButton.setOnClickListener(this);
        
        prefs = PreferenceManager.getDefaultSharedPreferences(this);
        prefs.registerOnSharedPreferenceChangeListener(this);
        
        validatePreferences();
    }
    
    public void onResume(){
    	super.onResume();
    	onServiceStateUpdate();
    }
    
    /**
     * Handle button clicks.
     */
    public void onClick(View src) {
        if(serviceButton.isPressed()){
        	if(Properties.SERVICE_RUNNING){
        		stopSubmitterService();
        	} else {
        		if(validatePreferences()){
	        		Properties.SERVICE_RUNNING = true;
	        		startService(new Intent(this, SubmitterService.class));
        		} else {
        			Toast.makeText(getApplicationContext(), getText(R.string.msg_missing), Toast.LENGTH_LONG).show();
        		}
        	}
        	onServiceStateUpdate();
        }
        if(settingsButton.isPressed()){
        	startActivity(new Intent(this, PrefsActivity.class));
        }
      }
    
    public boolean onCreateOptionsMenu(Menu menu){
    	MenuInflater inflater = getMenuInflater();
    	inflater.inflate(R.menu.menu, menu);
    	return true;
    }
    
    public boolean onOptionsItemSelected(MenuItem item){
    	switch(item.getItemId()){
    	case R.id.itemPrefs:
    		startActivity(new Intent(this, PrefsActivity.class));
    		break;
    	}
    	return true;
    }

	public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
		validatePreferences();
	}
	
	/**
	 * Validate the preferences
	 * 
	 * @return True if login data is complete. This does not mean they are valid.
	 */
	public boolean validatePreferences(){
		if(prefs.getString("password", "").equals("") || prefs.getString("username", "").equals("")){
			stopSubmitterService();
			return false;
		} else {
			serviceButton.setEnabled(true);
			return true;
		}
	}
	
	/**
	 * Change buttons when service state changes.
	 */
	public void onServiceStateUpdate(){
		if(Properties.SERVICE_RUNNING){
			Log.i("EneoActivity", "Service is running.");
			serviceButton.setImageResource(R.drawable.stop);
		} else {
			Log.i("EneoActivity", "Service is NOT running.");
			serviceButton.setImageResource(R.drawable.start);
		}
	}
	
	/**
	 * Stop the Submitter Service
	 * 
	 * The submitter service is stopped and the properties, 
	 * as well as the userinterface, are updated.
	 *
	 */
	public void stopSubmitterService(){
		stopService(new Intent(this, SubmitterService.class));
		Properties.SERVICE_RUNNING = false;
		onServiceStateUpdate();
	}
 
}